(function() {
  'use strict';

  define(["app", "services/StoreManager"], function(AMail) {

    AMail.factory('ListService', ["$http", "StoreManager", '$location', function($http, StoreManager, $location) {
      function getMessages() {
        var messages = StoreManager.getItem("AMail");

        if (messages) {
          return messages;
        }
        return [];
      }

      return {
        remove: function(index) {
          this.messages = getMessages();
          this.messages.splice(index, 1);
          StoreManager.setItem("AMail", this.messages);
        },
        newMessage: function() {
          $location.path("/new/" + new Date().getTime());
        },

        removeAll: function() {
          this.messages = getMessages();
          this.messages.splice(0, this.messages.length);
          StoreManager.setItem("AMail", this.messages);
        },

        reload: function(callback) {
          this.messages = getMessages();

          if (typeof callback === 'function') {
            callback.call(this);
          }
        },

      };
    }]);
  });
}).call(this);